//
//  FindClassifyViewController.m
//  Everest
//
//  Created by 李其昌 on 14/12/17.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "FindClassifyViewController.h"
#import "Classify.h"
#import "MyNavigationView.h"
#import "FindClassifyCollectionViewCell.h"
#import "NetHandler.h"
#import "UIImageView+WebCache.h"
#import "FindClassifyDetailViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "UIColor+AddColor.h"
#import "MMDrawerBarButtonItem.h"
#import "JingRoundView.h"
#import "AvReadioPlayer.h"


@interface FindClassifyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, JingRoundViewDelegate>

@property (nonatomic, retain) NSString *nameStr;
@property (nonatomic, retain) JingRoundView *roundView;
@property (nonatomic, retain) UIView *backView;
@end

@implementation FindClassifyViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        
        
    }
    return self;
}

- (void)handleData
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/category_tag_list?category=%@&device=iPhone&type=album", self.classify.name];
    
//    NSLog(@"%@", str);
    
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *arr = [resultDic objectForKey:@"list"];
        
        Classify *classs = [[Classify alloc] init];
        classs.name = @"all";
        classs.tname = @"全部";
        
        self.classifyArr = [NSMutableArray array];
        [self.classifyArr addObject:classs];
        for (NSDictionary *dic in arr) {
            
            Classify *class = [[Classify alloc] initWithDictionary:dic];
            class.name = self.classify.name;
            [self.classifyArr addObject:class];
            
            [class release];
            
        }
//        NSLog(@"%@", [[self.classifyArr objectAtIndex:0] tname]);
        [classs release];
        [self.collectionView reloadData];
    }];
    

}



- (void)dealloc
{
    [_roundView release];
    [_collect release];
    [_collectionView release];
    [_classifyArr release];
    [_myNavigationView release];
    [_classifyArr release];
    [super dealloc];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:1.0 blue:1.0 alpha:1.0];
//    self.view.backgroundColor = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tabBarController.tabBar.hidden = YES;
    [super.navigationController setNavigationBarHidden:NO];
    [self setupLeftMenuButton];
    self.title = self.classify.title;
    
    
    UICollectionViewFlowLayout *flowLatouy = [[UICollectionViewFlowLayout alloc] init];
    flowLatouy.itemSize = CGSizeMake((self.view.bounds.size.width - 40) / 3, 92.5);
    // 最小行间距
    flowLatouy.minimumLineSpacing = 10;
    // 最小item间距
    flowLatouy.minimumInteritemSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 71, self.view.bounds.size.width - 20, self.view.bounds.size.height) collectionViewLayout:flowLatouy];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    // 指定代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 150, 0);
    
    self.collectionView.showsVerticalScrollIndicator = NO;

    
    UIImageView *backView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableViewback.jpg"]];
    backView.userInteractionEnabled = YES;
    [self.view addSubview:backView];
    [backView addSubview:self.collectionView];
    [backView release];
    [_collectionView release];
    
    [self.collectionView registerClass:[FindClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    // 自定义tabbar
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49)];
    [self.view addSubview:self.backView];
    self.backView.backgroundColor = [UIColor tabBarColor];
    // 返回键
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backView addSubview:backButton];
    backButton.frame = CGRectMake(10, 10, 30, 29);
    [backButton setImage:[UIImage imageNamed:@"iconfont-gerenfanhui.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor sectionColor]];
    [backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [_backView release];
    
//     NSLog(@"%@", self.classify.name);
}
// 自定义tabbar中播放按钮方法
-(void)playStatuUpdate:(BOOL)playState
{
    NSLog(@"%@...", playState ? @"播放": @"暂停了");
    if (playState == YES) {
        NSLog(@"播放");
        AvReadioPlayer *avPlayer = [AvReadioPlayer defaultAvPlayer];
        [avPlayer play];
    } else {
        NSLog(@"暂停");
        AvReadioPlayer *avPlayer = [AvReadioPlayer defaultAvPlayer];
        [avPlayer pause];
        
    }
}
// 设置navigation的左按钮为抽屉开关
- (void)setupLeftMenuButton
{
    MMDrawerBarButtonItem * leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
    [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}
// 触发的方法
- (void)leftDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}


- (void)backButtonClicked:(UIButton *)button
{
    NSLog(@"返回上一页");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"return" object:nil userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    // 播放键
    self.roundView = [JingRoundView defaultJingRoundView];
    self.roundView.frame = CGRectMake((self.view.bounds.size.width / 2) - 30, - 10, 60, 60);
    self.roundView.delegate = self;
    self.roundView.rotationDuration = 4.0;
    [self.backView addSubview:self.roundView];
    
    
    [super.navigationController setNavigationBarHidden:NO];
    self.tabBarController.tabBar.hidden = YES;
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
}

- (void)viewWillDisappear:(BOOL)animated

{
    
    [super viewWillDisappear:animated];
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.classifyArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    Classify *class = [self.classifyArr objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0) {
        
        UIImage *image = [UIImage imageNamed:@"zhumulangma.jpeg"];
        
        cell.myImageView.image = image;
        
    } else {
    
        NSURL *url = [NSURL URLWithString:class.cover_path];
    
        [cell.myImageView setImageWithURL:url];
    }
    cell.myLabel.text = class.tname;
    cell.myLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}
// Cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindClassifyDetailViewController *findClassifyVC = [[FindClassifyDetailViewController alloc] init];
    
    Classify *classify = [self.classifyArr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:findClassifyVC animated:YES];
    
    findClassifyVC.classify = classify;
    findClassifyVC.collect = self.collect;
    
    [findClassifyVC release];
}

- (void)returnAction:(UIButton *)button
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"return" object:nil userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
